package sd.czw.com.miemiegoushop.activity;

import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import sd.czw.com.miemiegoushop.R;
import sd.czw.com.miemiegoushop.adapter.MyAdapter;
import sd.czw.com.miemiegoushop.adapter.MyPullAdapter;
import sd.czw.com.miemiegoushop.base.CzwBaseActivity;
import sd.czw.com.miemiegoushop.data.Contants;
import sd.czw.com.miemiegoushop.data.GoodsModelk;
import sd.czw.com.miemiegoushop.ui.MyCallBak;

/**
 * Created by czw
 * desc 描述：
 * on 2017/10/11.
 */
public class ActoRecycleViewActivity extends CzwBaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener{
    private Map<String, String> params = new HashMap<>();
    private List<GoodsModelk.ErrmsgBean> titles = new ArrayList<>();
    private MyAdapter myAdapter;
    private int start = 0;
    private int page = 0;
    private MyPullAdapter myPullAdapter;
    private int count = 0;
    private RecyclerView mSmartRecycleView;
    private int mCurrentCounter = 0;
    private final int TOTAL_COUNTER = 20;
    private boolean isErr = true;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    public int getLayoutId() {
        return R.layout.activity_autorecycle;
    }

    @Override
    public void initView() {
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        mSmartRecycleView = (RecyclerView)findViewById(R.id.autorecycleview);
        mSmartRecycleView.setLayoutManager(new GridLayoutManager(this,2));
        myPullAdapter = new MyPullAdapter(R.layout.item,titles);
        mSmartRecycleView.setAdapter(myPullAdapter);
        loaddatas(start);
        myPullAdapter.setOnLoadMoreListener(this,mSmartRecycleView);

    }

    private void loaddatas(int start) {
        params.put("time", Contants.Time);
        params.put("sign", Contants.Sign);
        params.put("start", String.valueOf(start));
        params.put("limit", "20");
        OkGo.<GoodsModelk>post("http://miemiegou.com/wap/app_index_getgoods.php")
                .cacheMode(CacheMode.REQUEST_FAILED_READ_CACHE)
                .cacheTime(3600 * 1000)
                .params(params)
                .execute(new MyCallBak<GoodsModelk>(GoodsModelk.class, this) {

                    @Override
                    public void onSuccess(Response<GoodsModelk> response) {
                        if (response.body().getErrcode() == 1) {
                            titles.addAll(response.body().getErrmsg());
                            myPullAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Response<GoodsModelk> response) {

                    }
                });
    }

    @Override
    public void initData() {

    }


    @Override
    public void onRefresh() {
        myPullAdapter.setEnableLoadMore(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                titles.clear();
                start = 0;
                loaddatas(start);
                myPullAdapter.setNewData(titles);
              //  isErr = false;
                mSwipeRefreshLayout.setRefreshing(false);
                myPullAdapter.setEnableLoadMore(true);
            }
        }, 1500);
    }

    @Override
    public void onLoadMoreRequested() {
        mSwipeRefreshLayout.setEnabled(false);
        if (myPullAdapter.getData().size() < 20) {
            myPullAdapter.loadMoreEnd(true);
        } else {
//                if (isErr) {
                    page++;
                    start = page * 20;
                    loaddatas(start);
                    //myPullAdapter.addData(titles);
                    mCurrentCounter = myPullAdapter.getData().size();
                    myPullAdapter.loadMoreComplete();
//                } else {
//                    isErr = true;
//                    Toast.makeText(ActoRecycleViewActivity.this, "Simulation network error", Toast.LENGTH_LONG).show();
//                    myPullAdapter.loadMoreFail();
//                }
            mSwipeRefreshLayout.setEnabled(true);
        }
    }
}
